package com.eascs.web.o2o.web.reward.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.context.RequestContext;
import com.eascs.web.o2o.web.reward.entity.LoanDetailForm;
import com.eascs.web.o2o.web.reward.service.ReportForLoanDetailService;

@RestController
@RequestMapping(value="restLoanDetail")
public class RewardLoanDetailRestController extends BaseRestController {
	
	@Autowired
	ReportForLoanDetailService reportForLoanDetailService;

	/**
	 * 贷款明细报表
	 * @param entity
	 * @return
	 */
	@RequestMapping(value="/loanDetailList")
	private PaginationRestResult loanDetailList(LoanDetailForm entity) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getCheckMonth()==null?date:entity.getCheckMonth();
		entity.setCheckMonth(month);
		String o2oUrl = "/restLoanDetail/loanDetailList";
		if(entity != null) {
			String checkMonth = entity.getCheckMonth();
			String provinceName = entity.getProvinceName();
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "checkMonth", checkMonth);
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "provinceName", provinceName);
			o2oUrl = o2oUrl + "&showPage=";
		} else {
			o2oUrl = o2oUrl + "?showPage=";
		}
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = reportForLoanDetailService.reportForLoanDetail(entity, "search");
		String[] titles = {"考核月份", "省区", "贷款单号", "借款人", "放款日期", "到期日期", "放款金额（万元）", "放款期限", "是否续贷", "折算后放款金额(万元)", "信贷经理名称", "推荐类型", "推荐人名称", "推荐人分佣系数", "信贷经理分成金额(万元)", "推荐人分成金额(万元)", "备注"};
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "showPage", ""));
		paginationInfo.setFirstRenderColumnIndex(0);
		paginationInfo.setFormats(new Format[] {null, null, null, null, null, null, new DecimalFormat(",##0.00"), null, null, new DecimalFormat(",##0.00"), null, null, null, null, new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), null});
		paginationInfo.setRendTo("#o2o_result");
		ViewContext.put("vo", entity);
		ViewContext.put("o2oUrl", o2oUrl);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
	
}
